package com.group9.handinhand.dao;

import java.util.List;

import org.springframework.stereotype.Repository;



import com.group9.handinhand.dao.model.Event;
import com.group9.handinhand.dao.model.Request;
import com.group9.handinhand.dao.model.RequestPost;
import com.group9.handinhand.dao.model.RequestResponse;
import com.group9.handinhand.dao.model.User;
import com.group9.handinhand.service.so.UserTagSO;
/**
 * Class for Request dao
 *
 */
@Repository
public class RequestDao extends BaseDaoImpl {
	
	public Request saveRequest(Request request) {
		save(request);
		return request;
	}
	/**
	 * finds request by request id
	 * @param requestId
	 * @return
	 */
	public Request findRequestById(Long requestId) {
		return (Request)findById(Request.class, requestId);
	}
	/**
	 * finds request by tag
	 * @param tag
	 * @return
	 */
	public List<Request> findRequestsByTag(UserTagSO tag) {
		String queryString = "Select distinct (ot.request) from RequestTag ot inner join ot.tag es where es.id =:tagId";
		return executeQueryString(queryString, "tagId", tag.getTag().getId());
	}
	/**
	 * finds request by its name
	 * @param keyword
	 * @return
	 */
	public List<Request> findRequestsByName (String keyword) {
		String queryString = "from Request rs where str(rs.name) like :keyWord";
		return executeQueryString(queryString, "keyWord", "%"+keyword+"%");
	}
	/**
	 * finds request by tag name
	 * @param keyword
	 * @return
	 */
	public List<Request> findRequestsByTagName(String keyword) {
		String queryString = "Select distinct (ot.request) from RequestTag ot inner join ot.tag es where es.tagName =:keyWord";
		return executeQueryString(queryString, "keyWord", keyword);
	}
	/**
	 * finds request that is responded by a user.
	 * @param user
	 * @return
	 */
	public List<Request> findRespondedRequestsByUser(User user) {
		String queryString = "Select distinct (ea.request) from RequestResponse ea inner join ea.user es where es.id =:userId";
		return executeQueryString(queryString, "userId", user.getId());
	}
	/**
	 * finds the request that is moderated by user
	 * @param user
	 * @return
	 */
	public List<Request> findModeratedRequestsByUser(User user) {
		String queryString = "Select distinct r from Request r inner join r.moderators rm where rm.id =:userId";
		return executeQueryString(queryString, "userId", user.getId());
	}
	
	public RequestPost saveRequestPost(RequestPost requestPost) {
		save(requestPost);
		return requestPost;
	}

	public RequestResponse saveRequestResponse(RequestResponse requestResponse) {
		save(requestResponse);
		return requestResponse;
	}
	/**
	 * finds request's response by a user
	 * @param userId
	 * @param requestId
	 * @return
	 */
	public List<RequestResponse> findRequestResponsesByUserIdAndRequestId(Long userId, Long requestId) {
		String queryString = "from RequestResponse as rr  where rr.user.id =:userId and rr.request.id =:requestId";
		return executeQueryString(queryString, new String[]{"userId" , "requestId"}, new Object[]{userId,requestId});
		
	}
	/**
	 * find moderators of the request
	 * @param requestId
	 * @return
	 */
	public List<User> findModeratorsByRequestId(Long requestId) {
		String queryString = "Select rm from Request r inner join r.moderators rm where r.id =:requestId";
		return executeQueryString(queryString, "requestId", requestId);
	}

	public Request updateRequest(Request request) {
		update(request);
		return request;
	}
	/**
	 * find moderated requests 
	 * @param requestId
	 * @param userId
	 * @return
	 */
	public Request findModeratedRequestByRequestIdAndUserId(Long requestId, Long userId) {
		String queryString = "Select r from Request r inner join r.moderators rm where rm.id =:userId and r.id =:requestId";
		return executeQueryStringForObject(queryString, new String[]{"requestId" , "userId"}, new Object[]{requestId,userId});
	}

}
